<?php
#
# author: Phil Spagnolo & Cody Sorgenfrey
# (c)2012 South Hill Calvary Chapel
#
include_once("../mysql/database_info.php");
include_once("common.php");
include_once("../mysql/common.php");

$title = "Prayer Request"; 
$meta_key= 'prayer, request, prayer chain, prayer request, praise, praise report, update';
$meta_desc= "Request prayer from the prayer team at South Hill Calvary Chapel";
$morehtml= NULL;
$morejava= NULL;

function textInBox(){
	
	if ($_GET['message']){
	
		return $_GET['message'];
	}
	else{
	
		return "Your Prayer Request/Praise Report.";
	}
}
function checkVar(){
	
	if ($_GET['fName']) $check = TRUE;
	else return "Missing first name";
	
	if ($_GET['lName']) $check = TRUE;
	else return "Missing last name";
	
	if ($_GET['email']){
		
		if (strstr($_GET['email'], "@") && strstr($_GET['email'], ".")){
			
			$check = TRUE;
		}
		else {
			
			return "Invalid Email.";
		}
	}
	else return "Missing Email.";
	
	if ($_GET['shcc']){
		
		if($_GET['shcc'] != "#"){
			
			$check = TRUE;
		}
		else{
			
			return "SHCC attendance cannot be \"Go to SHCC?\".";
		}
	}
	else return "Missing SHCC Attendance.";
	
	if ($_GET['isRequest']){
		
		if($_GET['isRequest'] != "#"){
			
			$check = TRUE;
		}
		else{
			
			return "Prayer Type cannot be \"Prayer Type:\".";
		}
	}
	else return "Missing Prayer Type.";
	
	if ($_GET['subject']) $check = TRUE;
	else return "Missing Subject.";
	
	if ($_GET['message']){
		
		if ($_GET['message'] != "Your Prayer Request/Praise Report."){
			
			$check = TRUE;
		}
		else{
			
			return "Please fill out your prayer request/praise report.";
		}
	} 
	else return "Missing prayer request/praise report.";
	
	if ($_GET['spam']){
		
		if ($_GET['spam'] == "SEND"){
			
			$check = TRUE;
		}
		else {
			
			return "Missing verification box (A.K.A. \"SEND\" box).";
		}
	}
	else return "Missing verification box (A.K.A. \"SEND\" box).";
	
	if ($check == TRUE) return "TRUE";
	
}

function printForm(){
	
	printf("<form action='/prayer_request.php'>\n");

	printf("\t<input type='text' name='fName' placeholder='First Name' value='%s' />\n", $_GET['fName']);
	
	printf("\t<input type='text' name='lName' placeholder='Last Name' value='%s' />\n", $_GET['lName']);
	
	printf("\t<input type='text' name='email' placeholder='Your Email' value='%s' />\n", $_GET['email']);
	
	printf("\t<select name='shcc'>\n");
	
	printf("\t\t<option value='#'>Do You Attend SHCC?</option>\n");
	
	printf("\t\t<option value='TRUE'>Yes</option>\n");
	
	printf("\t\t<option value='FALSE'>No</option>\n");
	
	printf("\t</select>\n");
	
	printf("\t<select name='isRequest'>\n");
	
	printf("\t\t<option value='#'>Type of Prayer:</option>\n");
	
	printf("\t\t<option value='TRUE'>Prayer Request</option>\n");
	
	printf("\t\t<option value='FALSE'>Praise Report</option>\n");
	
	printf("\t</select>\n");
	
	printf("<br><br>\n");
	
	printf("\t<input type='text' name='subject' placeholder='Prayer Request Subject' value='%s' /><br><br>\n", $_GET['subject']);
	
	printf("<textarea name='message' style='width:%s; min-height:200px;'>%s</textarea>\n", "100%", textInBox());
	
	printf("<p>To prevent spam, please type \"SEND\", in all capital letters, in the box: <input type='text' name='spam' placeholder='type to send' /></p>\n");
	
	printf("\t<input type='submit' value='Send' />\n");
	
	printf("</form>\n");

}

startPage($title, $meta_key, $meta_desc, $morehtml, $morejava);

if ($_GET['fName'] || $_GET['lName'] || $_GET['email'] || $_GET['shcc'] || $_GET['subject'] || $_GET['message'] || $_GET['spam']){

	if (checkVar() == "TRUE"){
		
		if ($_GET['isRequest'] == "TRUE"){
			
			$type = "prayer request";
		} 
		else{
			
			$type = "praise report";
		}
		
		if ($_GET['shcc'] == "TRUE"){
			
			$message = $type." from ".$_GET['fName']." ".$_GET['lName']." (".$_GET['email'].") who attends SHCC:  ".$_GET['message'];
		}
		else{
			
			$message = $type." from ".$_GET['fName']." ".$_GET['lName']." (".$_GET['email'].") who does not attend SHCC:  ".$_GET['message'];
		}
	
		$mailCheck = mail("prayer@southhillcalvary.org", $_GET['subject'], $message, "From: ".$_GET['email']);
		
		if ($mailCheck){
			
			printf("<p>%s, thank you for submitting your %s.  It is our honor to pray for you.  Please let us know how you are doing by reporting back.</p>\n", ucfirst($_GET['fName'])." ".ucfirst($_GET['lName']), $type);
		}
		else {
			
			printf("<p>We're sorry, there was an error processing your %s, please try again, and if the problem persists contact us here: <a href='mailto:webservant@southhillcalvary.org'>[webservant@southhillcalvary.org]</a>.</p>", $type);
		}
	}
	else{
		
		printf("<h4><font color='red'>".checkVar()."</font></h4>");
		
		printForm();
	}
}
else{
	
	printForm();
}

endPage();


?>